package yth.api.user.common;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import freemarker.core.ParseException;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.TemplateException;
import freemarker.template.TemplateNotFoundException;

@Component
@EnableConfigurationProperties(MailProperties.class)
public class JavaMailComponent {

	private static final String template="mail.ftl";
	
	@Autowired
	private FreeMarkerConfigurer freeMarkerConfigurer;
	
	@Autowired
	private JavaMailSender javaMailSender;
	
	@Autowired
	private MailProperties mailProperties;
	
	public void sendMail(String email,String msg) {
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("email", email);
		map.put("msg", msg);
		try {
			//获取内容
			String text=this.getTextByTemplate(template, map);
			//发送
			this.send(email, text);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private String getTextByTemplate(String template,Map<String, Object> model) throws TemplateNotFoundException, MalformedTemplateNameException, ParseException, IOException, TemplateException {
		return FreeMarkerTemplateUtils
				.processTemplateIntoString(this.freeMarkerConfigurer.getConfiguration().getTemplate(template), model);
	}
	
	private String send(String email,String text) throws MessagingException, UnsupportedEncodingException {
		MimeMessage message=this.javaMailSender.createMimeMessage();
		MimeMessageHelper helper=new MimeMessageHelper(message,true,"utf-8");
		
		InternetAddress from=new InternetAddress();
		from.setAddress(this.mailProperties.getUsername());
		from.setPersonal("高高高", "utf-8");
		
		helper.setFrom(from);
		helper.setTo(email);
		helper.setSubject("spring boot 发送的一封邮件");
		helper.setText(text,true);
		this.javaMailSender.send(message);
		
		return text;
	}
}
